
Ext.define('FrontSuite.view.ViewInfo', {
    extend:          'Ext.Panel',  // defines the superclass. data in class inheritance.

    id:              null,
    tabId:           null,

    styleHtmlContent: true,
    autoScroll:      true,

    constructor: function (tabId, viewDef) {

        this.id    = 'info-panel-' + tabId;
        this.tabId = tabId;
        this.title = viewDef.table_label;
        this.superclass.constructor.call(this, this.id);
        return(this);
    },

    listeners: {
        afterrender : function(obj){
            FrontSuite.util.ext.get({ type: 'help', key: 'general.help.panel' }, function (response, message) {
                if (response.success) {
                    Ext.getCmp('info-panel-' + obj.tabId).update(response.data);
                }
                else {
                    Ext.MessageBox.show({
                       title   : 'Status',
                       msg     : 'server-side failure: ' + message,
                       buttons : Ext.MessageBox.OK
                    });
                }
            });
        }
    },
    tools: [{
        type : 'edit',
        handler : function(event, toolEl, panel, tool){
            var tabId = tool.ownerCt.ownerCt.tabId;

            Ext.create('Ext.window.Window', {
                xtype  : 'panel',
                title  : 'Message / Info Editor - ' + Ext.getCmp('info-panel-' + tabId).title,
                width  : 550,
                height : 250,
                frame  : true,
                layout : 'fit',
                shadowOffset : 25,
                tools  : [{
                    type    : 'save',
                    handler : function(event, toolEl, panel){
                        var content = Ext.getCmp('info-panel-htmledit.' + tabId).getValue();
                        Ext.getCmp('info-panel-' + tabId).update(content);

                        Ext.Ajax.request({
                            url       : 'extjs-store.php',
                            params    : {type: 'help', op: 'put', key: tabId, data: content},
                            autoAbort : false,
                            success : function(response){
                                Ext.MessageBox.show({
                                   title   : 'Status',
                                   msg     : 'Changes saved successfully.',
                                   buttons : Ext.MessageBox.OK
                                });
                            },
                            failure: function(response, opts) {
                                Ext.MessageBox.show({
                                   title   : 'Status',
                                   msg     : 'server-side failure with status code ' + response.status,
                                   buttons : Ext.MessageBox.OK
                                });
                            }
                        });
                    }
                }],
                items : {
                    xtype            : 'htmleditor',
                    id               : 'info-panel-htmledit.' + tabId,
                    enableColors     : true,
                    enableAlignments : true
                }
            }).show();

            if (Ext.getCmp('info-panel-' + tabId).body.dom.innerHTML != null) {
                Ext.getCmp('info-panel-htmledit.' + tabId).setValue(Ext.getCmp('info-panel-' + tabId).body.dom.innerHTML);
            }
        }
    }]
});

